#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cl.h"
#include "client.h"
#include "../scanADT/scanadt.h"
#include "../scanADT/strlib.h"


static void
help(void)
{
	char msg[] =
	"\tlistar <huesped> -- listado de habitaciones para el huesped\n"
	"\treservar <huesped> -- reserva una habitacion para el huesped\n"
	"\tocupar <huesped> <numero> -- ocupa la habitacion deseada si fue reservada previamente\n"
	"\tliberar <huesped> <numero> -- libera la habitacion deseada si fue ocupada previamente\n"
	"\tayuda -- muestra esta ayuda\n"
	"\tsalir -- sale del programa\n"
	"\n";
	
	printf("%s", msg);
}

/* get input line */
int
input(char * prompt, char * inpbuf)
{
	char c;
	int count= 0;

	/* print prompt */
	printf("%s", prompt);

	forever
	{
		c= getchar();

		if( count < MAXLINE )
			inpbuf[count++]= c;

		if( c == '\n' && count < MAXLINE )
		{
			inpbuf[count]= '\0';
			return count;
		}
		if( c == '\n' )
		{
			printf("Input line too long (Max: 511 characters).\n");
			printf("%s", prompt);
			count= 0;
		}
	}
}

/* process input line */
int
process_line(char * line)
{
	int i, narg= 0;
	char * arg[MAXARGS+1];
	char * token= NULL;
	scannerADT scanner;

	if( checkLine(line) )
	{
		printf("HotelDB: no such command\n");
		return 0;
	}	
	scanner= NewScanner();
	SetScannerString(scanner, line);	

	while( MoreTokensExist(scanner) )
	{
		token= ReadToken(scanner);
		if( (arg[narg]= malloc(strlen(token))) == NULL )
		{
			freeArg(arg, narg-1);
			return 1;
		}
		strcpy(arg[narg++], token);
	}
	if( (i= process_command(narg, arg)) == 0 )
		printf("HotelDB: no such command\n");
	else if( i == EQUIT || i < 0 )
	{
		freeArg(arg, narg);
		return 1;
	}
	FreeScanner(scanner);
	freeArg(arg, narg);
	return 0;
}

int
checkLine(char * line)
{
	int count= 0;

	while( *line != '\0' )
	{
		if( count != 0 && *line == '"' && count%2 == 0 && *(line-1) != ' ' &&
		   *(line-1) != '\t')
			return 1;
		if( *line == '"' && count%2 != 0 && *(line+1) != ' ' &&
		   *(line+1) != '\n' && *(line+1) != '\t' )
			return 1;
		if( *line == '"' )
			count++;
		line++;
	}
	if( count%2 == 0 )
		return 0;
	return 1;
}

int
process_command(int argc, char * argv[])
{
	if( !argc )					/* If there are no arguments, do nothing */
		return 1;


	if (strcmp(STRHELP, argv[0]) == 0 ) /*help*/
	{
		if (argc != 1)
		{
			printf("For help type \"%s\"", STRHELP);
			return 1;
		}
		help();
		return EHELP;
	}
	
	if (strcmp(STRQUIT, argv[0]) == 0)					/* quit */
	{
		if (argc != 1)
		{
			printf("Usage: %s \n", STRQUIT);
			return 1;
		}
		return EQUIT;	
	}
	if (strcmp(STRFREE, argv[0]) == 0)				/* free room */
	{
		if (argc != 3)
		{
			printf("Usage: %s <huesped> <numero>\n", STRFREE);
			return 1;
		}
		liberar(argv[1], argv[2], _HDB_);
		return EFREE;
	}
	else if (strcmp(STROCUP, argv[0]) == 0)			/* occupy room */
	{
		if (argc != 3)
		{
			printf("Usage: %s <huesped> <numero>\n", STROCUP);
			return 1;
		}
		ocupar(argv[1], _HDB_, argv[2]);
		return EOCUP;
	}
	else if (strcmp(STRRSVR, argv[0]) == 0)			/* reserve room*/
	{
		if (argc != 2)
		{
			printf("Usage: %s <huesped>\n", STRRSVR);
			return 1;
		}
		reservar(argv[1], _HDB_);
		return ERSVR;
	}
	else if (strcmp(STRLIST , argv[0]) == 0)		/* list rooms for guest */
	{
		if (argc != 2)
		{
			printf("Usage: %s <huesped>\n", STRLIST);
			return 1;
		}		
		listar(argv[1], _HDB_);
		return ELIST;
	}
	return 0;
}


void
freeArg(char ** arg, int narg)
{
	int i;

	if( narg < 1 )
		return;
	for( i= 0; i < narg; i++ )
		free(arg[i]);
	return;
}

